import { WEAPON_TYPE } from "./Core/AppDef";

const {ccclass, property} = cc._decorator;


@ccclass
export default class Bullet extends cc.Component {
    @property(cc.SpriteFrame)
    spriteFrames:cc.SpriteFrame[] = []

    p = cc.v3(0,0,0);
    bulletType : WEAPON_TYPE = WEAPON_TYPE.My;
    from = 0;

    onLoad(){
        this.onReset();
        this.node.getComponent(cc.RigidBody).enabledContactListener = true;
    }
    
    onReset(){
        this.onUnScheduleMove();
        this.p = cc.v3(0,0,0);
        this.bulletType = WEAPON_TYPE.My;
        this.from = 0;
    }

    setP (p) {
        // this.p = p.multiplyScalar(1);
        this.p = p;
    }


    setBulletType (bulletType, from) {
        this.bulletType = bulletType;
        this.node.getComponent(cc.Sprite).spriteFrame = this.spriteFrames[WEAPON_TYPE.My];

        // if (bulletType == WEAPON_TYPE.My){
        //     cc.resources.load(BULLET_1, cc.SpriteFrame, null, function (err, spriteFrame) {
        //     });
        // }else {
        //     cc.resources.load(BULLET_2, cc.SpriteFrame, null, function (err, spriteFrame) {
        //         this.node.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        //     });
        // }
        this.from = from;

        this.onScheduleMove();
    }

    onMove () {
        cc.log("_--onMove  this.p.x ", this.p.x, "  this.p.y ", this.p.y)
        this.node.position = this.node.position.add(this.p);
    }

    onScheduleMove () {
        this.schedule(()=>{
            this.onMove();
        }, 0.02)
    }
    
    onUnScheduleMove () {
        cc.log("_--onUnScheduleMove")
        this.unscheduleAllCallbacks();
        // this.unschedule(()=>{
        //     this.onMove();
        // })
        this.p = cc.v3(0,0,0);
    }
}
